package com.dengdai.applibrary.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.dengdai.applibrary.R;
import com.dengdai.applibrary.adapter.TopPageAdapter;
import com.dengdai.applibrary.view.custom.CustomTabViewPager;
import com.dengdai.applibrary.view.custom.PagerSlidingTabStrip;

/**
 * @说明 头部选择器基类
 * @作者 LY
 * @时间 15/12/23 14:06
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public abstract class BaseTopPageFragment extends BaseFragment implements IBaseTopPageFragment {
    private PagerSlidingTabStrip top_page;
    private ImageView iv_add;
    private CustomTabViewPager custom_tab_viewpager;
    private TopPageAdapter topPageAdapter;

    @Override
    public int getLayoutResID() {
        return R.layout.common_base_toppage_fragment;
    }

    @Override
    public void initView(View view) {
        top_page = (PagerSlidingTabStrip) view.findViewById(R.id.top_page);
        iv_add = (ImageView) view.findViewById(R.id.iv_add);
        custom_tab_viewpager = (CustomTabViewPager) view.findViewById(R.id.custom_tab_viewpager);
        top_page.setBackgroundColor(getResources().getColor(R.color.top_page_bg));
        top_page.setDividerColorResource(R.color.top_page_bg);
        top_page.setUnderlineColorResource(android.R.color.transparent);
        top_page.setIndicatorColor(getResources().getColor(R.color.top_page_indicator_color));
        top_page.setShouldExpand(isShouldExpand());
        top_page.setIndicatorHeight(getResources().getDimensionPixelSize(R.dimen.top_page_indicator_height));
        top_page.setTextColor(getResources().getColor(R.color.top_page_text_nomal_color));
        top_page.setTextSelectColor(getResources().getColor(R.color.top_page_indicator_color));
        top_page.setTextSize(getResources().getDimensionPixelSize(R.dimen.top_page_title_size));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.top_page_right));
        if (isAdd()) {
            params.setMargins(0, 0, getResources().getDimensionPixelSize(R.dimen.top_page_right), 0);
            top_page.setLayoutParams(params);
            iv_add.setVisibility(View.VISIBLE);
        } else {
            params.setMargins(0, 0, 0, 0);
            top_page.setLayoutParams(params);
            iv_add.setVisibility(View.GONE);
        }
    }

    @Override
    public void business(Context context) {
        topPageAdapter = new TopPageAdapter(getChildFragmentManager());
        topPageAdapter.listTop.clear();
        topPageAdapter.listTop.addAll(getTopInfo());
        topPageAdapter.listFragment.clear();
        topPageAdapter.listFragment.addAll(getContentFragment());
        custom_tab_viewpager.setOffscreenPageLimit(topPageAdapter.listFragment.size() - 1);
        custom_tab_viewpager.setAdapter(topPageAdapter);
        top_page.setViewPager(custom_tab_viewpager);
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addTopTab();
            }
        });
    }
}
